//
//  ScroButton.m
//  ZZLogistics
//
//  Created by 辰领科技 on 15/12/28.
//  Copyright © 2015年 辰领科技. All rights reserved.
//

#import "ScroButton.h"

@interface ScroButton ()
@property(nonatomic,strong)UIView * button;
@property(nonatomic,strong)UIButton * backView;
@property(nonatomic,strong)UIColor *selectedColor;
@property(nonatomic,strong)UIColor *cancelColor;
@property(nonatomic,strong)UILabel * title;
@property(nonatomic,strong)void(^complect)(NSInteger index);
@property(nonatomic,strong)UIButton * editButton;
@property(nonatomic,strong)NSString * selectedTitle;
@property(nonatomic,strong)NSString * titleStr;
@property(nonatomic,assign)BOOL oval;
@end

@implementation ScroButton

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title selectedTitle:(NSString*)selecedTitle selectedColor:(UIColor *)selectedColor cancelColor:(UIColor *)cancelColor selected:(BOOL)selected oval:(BOOL)oval complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = ((CGRectGetHeight(self.frame)-1*VerticalRatio())/2)* VerticalRatio();
        _complect = complect;
        _ifSelected = selected;
        _cancelColor = cancelColor;
        _selectedColor = selectedColor;
        _titleStr = title;
        _selectedTitle = selecedTitle;
        _oval = oval;
        
        _backView = [UIButton buttonWithType:UIButtonTypeCustom];
        _backView.backgroundColor = cancelColor;
        _backView.layer.cornerRadius = (CGRectGetHeight(self.frame)-1*VerticalRatio())/2*VerticalRatio();
        _backView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        [self addSubview:_backView];
        
        _title = [[UILabel alloc]initWithFrame:CGRectMake(8*VerticalRatio(), 0, frame.size.width-16*VerticalRatio(), frame.size.height)];
        _title.textColor = [UIColor whiteColor];
        _title.text = title;
        _title.font = [UIFont systemFontOfSize:16*VerticalTexeRatio()];
        [self addSubview:_title];
        
        _button = [[UIView alloc]init];
        _button.backgroundColor = [UIColor whiteColor];
        _button.layer.cornerRadius = ((CGRectGetHeight(self.frame)-1*VerticalRatio())/2)*VerticalRatio();
        [_backView addSubview:_button];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.layer.cornerRadius = ((CGRectGetHeight(self.frame)-1*VerticalRatio())/2)*VerticalRatio();
        button.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
        _editButton = button;
        [_backView addSubview:button];
        
        [button addTarget:self action:@selector(changeIndex: ) forControlEvents:UIControlEventTouchUpInside];
        //
        if (selected == YES) {
            _button.frame =   CGRectMake(CGRectGetWidth(self.frame)-(CGRectGetHeight(self.frame)-0.5*VerticalRatio()), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
            
            if (_oval==YES) {
                _button.frame =   CGRectMake((CGRectGetWidth(self.frame)-1*VerticalRatio())/2, 0.5*VerticalRatio(), (CGRectGetWidth(self.frame)-1*VerticalRatio())/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
            }
            _backView.backgroundColor = _selectedColor;
            button.selected = YES;
            _title.text = _selectedTitle;
            _title.textAlignment = NSTextAlignmentLeft;
            
            _complect(1);
        }else{
            _button.frame =  CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
            if (_oval==YES) {
                _button.frame =   CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetWidth(self.frame)-1*VerticalRatio()/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
            }
            _backView.backgroundColor = _cancelColor;
            _title.text = _titleStr;
            _title.textAlignment = NSTextAlignmentRight;
            
            button.selected = NO;
            _complect(0);
            
        }
        
        
    }
    
    return self;
}


- (void)changeIndex:(UIButton *)sender{
    sender.selected = !sender.selected;
    _ifSelected = sender.selected;
    if (sender.selected == YES) {
        _button.frame =   CGRectMake(CGRectGetWidth(self.frame)-(CGRectGetHeight(self.frame)-0.5*VerticalRatio()), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
        if (_oval==YES) {
            if (_oval==YES) {
                _button.frame =   CGRectMake((CGRectGetWidth(self.frame)-1*VerticalRatio())/2, 0.5*VerticalRatio(), (CGRectGetWidth(self.frame)-1*VerticalRatio())/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
            }
        }
        _backView.backgroundColor = _selectedColor;
        _title.textAlignment = NSTextAlignmentLeft;
        _title.text = _selectedTitle;
        _complect(1);
    }else{
        _button.frame =   CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
        if (_oval==YES) {
            _button.frame =   CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetWidth(self.frame)-1*VerticalRatio()/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
        }
        _backView.backgroundColor = _cancelColor;
        _title.textAlignment = NSTextAlignmentRight;
        _title.text = _titleStr;
        _complect(0);
    }
    
}

- (void)setSelected:(BOOL)newSelected{
    
    if (newSelected == YES) {
        _button.frame =   CGRectMake(CGRectGetWidth(self.frame)-(CGRectGetHeight(self.frame)-0.5*VerticalRatio()), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
        if (_oval==YES) {
            if (_oval==YES) {
                _button.frame =   CGRectMake((CGRectGetWidth(self.frame)-1*VerticalRatio())/2, 0.5*VerticalRatio(), (CGRectGetWidth(self.frame)-1*VerticalRatio())/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
            }
        }
        _backView.backgroundColor = _selectedColor;
        _title.textAlignment = NSTextAlignmentLeft;
        _title.text = _selectedTitle;
    }else{
        _button.frame =  CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
        if (_oval==YES) {
            _button.frame =   CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetWidth(self.frame)-1*VerticalRatio()/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
        }
        _backView.backgroundColor = _cancelColor;
        _title.textAlignment = NSTextAlignmentRight;
        _title.text = _titleStr;
        
    }
}

- (void)setIfSelected:(BOOL)ifSelected{
    _ifSelected = ifSelected;
    if (ifSelected == YES) {
        _button.frame =   CGRectMake(CGRectGetWidth(self.frame)-(CGRectGetHeight(self.frame)-0.5*VerticalRatio()), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
        if (_oval==YES) {
            if (_oval==YES) {
                _button.frame =   CGRectMake((CGRectGetWidth(self.frame)-1*VerticalRatio())/2, 0.5*VerticalRatio(), (CGRectGetWidth(self.frame)-1*VerticalRatio())/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
            }
        }
        _backView.backgroundColor = _selectedColor;
        _title.textAlignment = NSTextAlignmentLeft;
        _title.text = _selectedTitle;
        
    }else{
        _button.frame =  CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio(), CGRectGetHeight(self.frame)-1*VerticalRatio());
        if (_oval==YES) {
            _button.frame =   CGRectMake(0.5*VerticalRatio(), 0.5*VerticalRatio(), CGRectGetWidth(self.frame)-1*VerticalRatio()/2, CGRectGetHeight(self.frame)-1*VerticalRatio());
        }
        _backView.backgroundColor = _cancelColor;
        _title.textAlignment = NSTextAlignmentRight;
        _title.text = _titleStr;
    }
}

- (BOOL)ifSelected{
    return _ifSelected;
}
@end
